package com.example.lycdays.ui.index;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lycdays.BaseApplication;
import com.example.lycdays.R;
import com.example.lycdays.data.bean.Event;
import com.example.lycdays.ui.detail.DetailActivity;
import com.example.lycdays.util.TimeTool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class IndexAdapter extends RecyclerView.Adapter<IndexAdapter.IndexViewHolder> {

    List<Event> allEvents = new ArrayList<>();

    public void setAllEvents(List<Event> allEvents) {
        this.allEvents = allEvents;
    }

    @NonNull
    @Override
    public IndexViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        final View itemView;
        itemView = layoutInflater.inflate(R.layout.cell_card, parent, false);
        final IndexViewHolder holder = new IndexViewHolder(itemView);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(BaseApplication.getContext(), "click", Toast.LENGTH_LONG).show();
                int position = holder.getAdapterPosition();
                Event event = allEvents.get(position);

                Intent intent = new Intent(BaseApplication.getContext(), DetailActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                intent.putExtra("event", event);
                BaseApplication.getContext().startActivity(intent);

            }
        });


        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull IndexViewHolder holder, int position) {

        Event event = allEvents.get(position);
        holder.textViewNumber.setText(String.valueOf(position + 1));
        holder.textViewName.setText(event.getEventName());
        holder.textViewDate.setText(event.getEventDate());

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = format.format(new Date());

        int eventGap = TimeTool.caculateDate(nowDate, event.getEventDate());
        String eventGapStr = null;
        if (eventGap > 0) {
            eventGapStr = "已经" + eventGap + "天";
        } else if (eventGap < 0) {
            eventGapStr = "还有" + -eventGap + "天";
        } else {
            eventGapStr = "就在今天";
        }

        holder.textViewGap.setText(eventGapStr);
    }

    @Override
    public int getItemCount() {
        return allEvents.size();
    }


    static class IndexViewHolder extends RecyclerView.ViewHolder {

        TextView textViewNumber, textViewName, textViewDate, textViewGap;

        public IndexViewHolder(@NonNull View itemView) {
            super(itemView);

            textViewNumber = itemView.findViewById(R.id.textViewNumber);
            textViewName = itemView.findViewById(R.id.textViewName);
            textViewDate = itemView.findViewById(R.id.textViewDate);
            textViewGap = itemView.findViewById(R.id.textViewGap);


        }
    }


}

